﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using QTMusic.BaseClasses;

namespace QTMusic.UserControls
{
    public partial class ActivityListItem : System.Web.UI.UserControl
    {
        private int activityId = 0;

        public int ActivityId
        {
            get { return activityId; }
            set { activityId = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string selectActivity = "SELECT activity.*, [user].user_id, [user].user_name, [user].user_pic "
                                    + "FROM [activity], [user] "
                                    + "WHERE activity_id = @activityId AND activity.user_id = [user].user_id";
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand cmdActivity = new SqlCommand(selectActivity, conn);
            cmdActivity.Parameters.AddWithValue("@activityId", activityId);
            SqlDataReader reader;

            try
            {
                using (conn)
                {
                    conn.Open();
                    reader = cmdActivity.ExecuteReader();
                    reader.Read();

                    string urlUser = Common.urlWebsites + "Profile.aspx?id=" + ((int)reader["user_id"]).ToString();
                    
                    // display user image
                    if (reader["user_pic"] != DBNull.Value)
                    {
                        imgUser.ImageUrl = (string)reader["user_pic"];
                        lnkUserImage.NavigateUrl = urlUser;
                    }
                    else
                        imgUser.ImageUrl = Common.defaultImage;
                    imgUser.Height = Unit.Pixel(50);
                    imgUser.Width = Unit.Pixel(50);

                    // display user name
                    if (reader["user_name"] != DBNull.Value)
                    {
                        lnkUserName.Text = (string)reader["user_name"];
                        lnkUserName.NavigateUrl = urlUser;
                    }
                    else
                        lnkUserName.Text = "Unknown";

                    // display time of the activity
                    LiteralControl objPanelTime = new LiteralControl(((DateTime)reader["time"]).ToString());
                    pnlActivityTime.Controls.Add(objPanelTime);

                    if (reader["playlist_up_id"] != DBNull.Value)
                    {

                    }
                    else if (reader["playlist_create_id"] != DBNull.Value)
                    {

                    }
                    else if (reader["share_id"] != DBNull.Value)
                    {
                        string selectSQL = "SELECT share_id, share.song_id, share_description, "
                            + "song.song_id, song.song_name, song.artist_id, artist.artist_id, artist.artist_name "
                            + "FROM [share], [song], [artist] "
                            + "WHERE [share].share_id = @shareId "
                            + "AND [song].song_id = [share].song_id "
                            + "AND [artist].artist_id = [song].artist_id";
                        SqlCommand cmd = new SqlCommand(selectSQL, conn);
                        cmd.Parameters.AddWithValue("@shareId", (int)reader["share_id"]);
                        conn.Close();

                        conn.Open();
                        reader = cmd.ExecuteReader();
                        reader.Read();

                        if (reader.HasRows)
                        {
                            // display activity
                            LiteralControl objPanelActivity = new LiteralControl(" đã chia sẻ bản nhạc này");
                            pnlActivity.Controls.Add(objPanelActivity);

                            // display link to the song
                            string urlSong = Common.urlWebsites + "Playing.aspx?SongID=" + ((int)reader["song_id"]).ToString();
                            lnkActivity.NavigateUrl = urlSong;
                            lnkActivity.Text = (string)reader["artist_name"] + " - " + (string)reader["song_name"];

                            // display description of the post
                            if (reader["song_description"] != DBNull.Value)
                            {
                                LiteralControl objPanelDescription = new LiteralControl((string)reader["share_description"]);
                                pnlDescription.Controls.Add(objPanelDescription);
                            }
                        }

                        conn.Close();
                    }
                    else if (reader["song_up_id"] != DBNull.Value)
                    {
                        string selectSQL = "SELECT song_id, song_name, song_description, "
                            + "song.artist_id, artist.artist_id, artist.artist_name "
                            + "FROM [song], [artist] "
                            + "WHERE song_id = @songId "
                            + "AND [artist].artist_id = [song].artist_id";
                        SqlCommand cmd = new SqlCommand(selectSQL, conn);
                        cmd.Parameters.AddWithValue("@songId", (int)reader["song_up_id"]);
                        conn.Close();

                        conn.Open();
                        reader = cmd.ExecuteReader();
                        reader.Read();

                        if (reader.HasRows)
                        {
                            // display activity
                            LiteralControl objPanelActivity = new LiteralControl(" đã tải lên bản nhạc này");
                            pnlActivity.Controls.Add(objPanelActivity);

                            // display link to the song
                            string urlSong = Common.urlWebsites + "Playing.aspx?SongID=" + ((int)reader["song_id"]).ToString();
                            lnkActivity.NavigateUrl = urlSong;
                            lnkActivity.Text = (string)reader["artist_name"] + " - " + (string)reader["song_name"];

                            // display description of the post
                            if (reader["song_description"] != DBNull.Value)
                            {
                                LiteralControl objPanelDescription = new LiteralControl((string)reader["song_description"]);
                                pnlDescription.Controls.Add(objPanelDescription);
                            }
                        }

                        conn.Close();
                    }
                }
            }
            catch (Exception activityListEx)
            {
                //ActivityListItemException newException = new ActivityListItemException(activityListEx.Message, activityListEx);
                //throw newException;
                Common.WriteToEventLog(activityListEx, "LoadActivityListItem");
            }
        }
    }

    public class ActivityListItemException : ApplicationException
    {
        public ActivityListItemException()
            : base()
        {

        }

        public ActivityListItemException(string message)
            : base(message)
        {

        }

        public ActivityListItemException(string message, Exception inner)
            : base(message, inner)
        {

        }
    }
}